package logic

import (
	"context"
	"errors"
	"shopping-cart/models/mysql"

	"shopping-cart/shoprpc/internal/svc"
	"shopping-cart/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CarAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCarAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CarAddLogic {
	return &CarAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CarAddLogic) CarAdd(in *shoprpc.CarAddRequest) (*shoprpc.CarAddResponse, error) {
	// todo: add your logic here and delete this line

	var car mysql.Car
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&car)

	if car.UserId == 0 {
		return nil, errors.New("用户不存在")
	}
	if car.ShopId == 0 {
		return nil, errors.New("商品不存在")
	}
	if car.KuCunId == 0 {
		return nil, errors.New("商品库存不存在")
	}

	car = mysql.Car{
		UserId:  int(in.UserId),
		ShopId:  int(in.ShopId),
		KuCunId: int(in.KuCunId),
		KuNum:   int(in.KuNum),
	}
	mysql.DB.Debug().Create(&car)

	return &shoprpc.CarAddResponse{
		CarId: int64(car.ID),
	}, nil
}
